﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using ServiceStack.Text;

namespace Monitor
{
    public class ServiceStackTextConfig
    {
        public static void Register(HttpConfiguration configuration)
        {
            configuration.Formatters.RemoveAt(0);
            configuration.Formatters.Insert(0, new ServiceStackTextFormatter());
        }
        public class ServiceStackTextFormatter : MediaTypeFormatter
        {
            public ServiceStackTextFormatter()
            {
                JsConfig.DateHandler = JsonDateHandler.ISO8601;
                SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
                SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/json"));
                SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
                SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true));
            }

            public override bool CanReadType(Type type)
            {
                if (type == null)
                {
                    throw new ArgumentNullException("type");
                }
                return true;
            }

            public override bool CanWriteType(Type type)
            {
                if (type == null)
                {
                    throw new ArgumentNullException("type");
                }
                return true;
            }

            public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, System.Net.Http.HttpContent content, IFormatterLogger formatterLogger)
            {
                var task = Task<object>.Factory.StartNew(() => JsonSerializer.DeserializeFromStream(type, readStream));
                return task;
            }

            public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, TransportContext transportContext)
            {
                var task = Task.Factory.StartNew(() => JsonSerializer.SerializeToStream(value, type, writeStream));
                return task;
            }
        }
    }
}